package com.ssy.lingxi.common.utils;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;

import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 用户Token生成工具类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-05
 */
public class TokenUtil {
    private static TokenUtil instance = new TokenUtil();

    private TokenUtil() {
    }

    public static TokenUtil instance() {
        return instance;
    }

    private Random random = new Random();

    private final int randomBound = 100000000;

    public String getToken() {
        try {
            String millsStr = String.valueOf(System.currentTimeMillis());
            String randomStr = String.valueOf(random.nextInt(randomBound));
            return MD5Util.Instance().MD32bitToHexString(millsStr.concat(randomStr));
        } catch (Exception e) {
            throw new BusinessException(ResponseCode.BUSINESS_ERROR);
        }
    }
}
